package testing;

import java.util.Vector;

import clang.control.CodeBlock;
import clang.control.ExecutionThread;
import clang.control.ForLoop;
import clang.control.ReturnStatement;
import clang.control.VariableDefinition;
import clang.data.Expression;
import clang.data.VarName;
import clang.data.VarType;
import clang.data.Variable;
import clang.functions.Function;
import clang.functions.FunctionCall;
import clang.ops.Operator;

public class ForLoopTest {

	public static void main(String[] args) {
		CodeBlock cb = new CodeBlock(ExecutionThread.get().getCurrentBlock());
		
		Variable msg = Variable.generateArray(cb, "In-loop : i = %d; j = %d\n", "myArray");
		
		cb.append(new VariableDefinition("i", VarType.INT));
		cb.append(new VariableDefinition("j", VarType.INT));
		
		CodeBlock iblock = new CodeBlock(cb);		
		Vector<Expression> pfargs = new Vector<Expression>();
		pfargs.add(msg);
		pfargs.add(new VarName("i"));
		pfargs.add(new VarName("j"));
		FunctionCall fcprintf = new FunctionCall("printf", pfargs);
		iblock.append(fcprintf);
		
		CodeBlock fblock = new CodeBlock(cb);		
		fblock.append(new ForLoop(iblock, new Operator("=", new VarName("j"), new Variable(0)), 
				new Operator("<", new VarName("j"), new Variable(5)), new Operator("x++", new VarName("j"))));
				
		cb.append(new ForLoop(fblock, new Operator("=", new VarName("i"), new Variable(0)), 
				new Operator("<", new VarName("i"), new Variable(3)), new Operator("x++", new VarName("i"))));
		
		cb.append(new ReturnStatement(0));
		
		Function mainFunc = new Function("main");
		mainFunc.setReturnType(VarType.INT);
		mainFunc.setBlock(cb);
		ExecutionThread.get().addFunction(mainFunc);
		
		try {
			ExecutionThread.get().runProgram();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
